package cn.evun.proabslistview;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ilioili.proabslist.AbsFreshView;
import com.laoxinwen.app.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FreshView implements AbsFreshView {
    private Activity activity;
    private LinearLayout freshView;
    private String lastUpdatetime;
    private Matrix m = new Matrix();
    /**
     * 箭头图片
     */
    private ProgressBar pb;
    /**
     * 最后更新时间的TextView
     */
    private TextView mHeaderTimeView;
    /**
     * 状态提示TextView
     */
    private TextView mHintTextView;
    private SharedPreferences sp;

    public FreshView(Activity a) {
        activity = a;
        sp = a.getSharedPreferences(a.getClass().getName(), Context.MODE_PRIVATE);
        FrameLayout root = new FrameLayout(activity);
        freshView = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.proabslistview_header, root, false);
        pb = (ProgressBar) freshView.findViewById(R.id.pull_to_refresh_header_arrow);
        mHintTextView = (TextView) freshView.findViewById(R.id.pull_to_refresh_header_hint_textview);
        mHeaderTimeView = (TextView) freshView.findViewById(R.id.pull_to_refresh_header_time);
        lastUpdatetime = sp.getString("last_time_update", "无数据刷新记录");
        mHeaderTimeView.setText(lastUpdatetime);
    }

    public View getFreshView() {
        return freshView;
    }

    public void onScrollWhileFreshViewVisible(int percentage) {
        if (percentage <= 100) {
            mHintTextView.setText("下拉可以刷新");
        } else {
            mHintTextView.setText("松手开始刷新");
        }
        pb.setVisibility(View.GONE);
    }

    public void onFreshing() {
        mHintTextView.setText("数据刷新中");
        pb.setVisibility(View.VISIBLE);
    }

    public void onFreshCompleted() {
        SimpleDateFormat formater = new SimpleDateFormat("最后一次刷新：yyyy年MM月dd日 HH:mm");
        lastUpdatetime = formater.format(new Date(System.currentTimeMillis())).toString();
        sp.edit().putString("last_time_update", lastUpdatetime).commit();
        mHeaderTimeView.setText(lastUpdatetime);
        mHintTextView.setText("数据已刷新");
        pb.setVisibility(View.GONE);
    }

}
